package bmi;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class BMIClient extends Application {
    DataOutputStream toServer = null;
    DataInputStream fromServer = null;

    @Override  // 重写Application类的start方法
    public void start(Stage primaryStage) throws Exception {
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        //行列之间的间隔
        grid.setHgap(10);
        grid.setVgap(10);
        //面板边缘周围的间隔
        grid.setPadding(new Insets(0, 25, 25, 25));

        Text sceneTitle = new Text("请输入您的身高体重，以便检测您的BMI指数：");
        sceneTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
        grid.add(sceneTitle, 0, 0, 2, 1);

        Label heightLable = new Label("身高:");
        grid.add(heightLable, 0, 2);
        TextField heightField = new TextField();
        heightField.setPromptText("身高（m）");
        grid.add(heightField, 1, 2);

        Label weightLabel = new Label("体重:");
        grid.add(weightLabel, 0, 3);
        TextField weightField = new TextField();
        weightField.setPromptText("体重（kg）");
        grid.add(weightField, 1, 3);

        // 添加按钮
        Button button = new Button("计算BMI指数");
        HBox hbBtn = new HBox(10);
        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn.getChildren().add(button);
        grid.add(hbBtn, 1, 4);


        // 显示内容文本区域
        Label text = new Label("检测结果：");
        grid.add(text, 0, 4);
        TextArea textBMI = new TextArea();
        grid.add(textBMI, 1, 5);


        // 创建一个场景并放到舞台上
        Scene scene = new Scene(grid, 600, 400);
        primaryStage.setTitle("BMIClient");  // 设置舞台（窗口）的标题
        primaryStage.setScene(scene);  // 把场景放到舞台上
        primaryStage.show(); //展示舞台

        button.setOnAction(e -> {
            try {
                // 从文本字段获取身高体重
                double height = Double.parseDouble(heightField.getText().trim());
                double weight = Double.parseDouble(weightField.getText().trim());

                // 从服务端发送身高体重
                toServer.writeDouble(height);
                toServer.writeDouble(weight);
                toServer.flush();

                // 从服务端获取BMI
                double BMI = fromServer.readDouble();
                String result = fromServer.readUTF();

                textBMI.appendText("身高：" + height + "\n");
                textBMI.appendText("体重：" + weight + "\n");
                textBMI.appendText("BMI：" + BMI + "\n" + "体质属于：" + result + "\n"); // 服务端数据


            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });

        try {
            // 创建一个socket连接到服务端
            Socket socket = new Socket("localhost", 8000);
            // Socket socket = new Socket("ip地址",8000);
            // Socket socket = new Socket("drake.Armstrong.edu",8000);

            // 创建一个输入流从服务端接收数据
            fromServer = new DataInputStream(socket.getInputStream());
            // 创建一个输出流从服务端发送数据
            toServer = new DataOutputStream(socket.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        launch(args);
    }
}
